/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = ibmmq_put_analysis.c                               */
/*  NOTE      =                                                    */
/*  DATE      = 2016/04/08 by jikw                                 */
/*******************************************************************/
#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frct_comm.h"
#include "../../../src/module/ibmmq/config.h"
#include "../test_ibmmq_comm.h"
#include <apmtest_comm.h>
#include <frreport_opt.h>
#include <ibmmq/cnaps2.h>
#include <ibmmq/ibmmq_core.h>

static int __suite_init(void)
{
	return 0;
}

static int __suite_cleanup(void)
{
	return 0;
}

static void read_file(char *p_filename, char **p_out, int *p_i_len)
{
    FILE *p_file = NULL;
    //struct stat buf;
    char *p_data = NULL;
    int i_len = 0;

    p_file = fopen(p_filename, "r");
    fseek(p_file, 0, SEEK_END);
    i_len = ftell(p_file);
    rewind(p_file);

    p_data = (char *)frmalloc(i_len + 1);
    memset(p_data, 0x00, i_len + 1);
    fread(p_data, 1, i_len, p_file);
    fclose(p_file);

    *p_out = p_data;
    *p_i_len = i_len;

    return;
}

extern int ibmmq_put_analysis(Frreport_body_data *p_st_body_data
                               , L7session_ibmmq_private *p_st_private
                               , Frbyte_Packet *p_st_data
                               , unsigned int ui_data_len
                               , char *p_report_pre
                               , int i_type);

extern int ibmmq_analysis_head(char *p_data
                      , unsigned int ui_head_len
                      , L7_head_ibmmq **pp_st_head);

static void case1(void)
{
    Frreport_body_data *p_st_body_data = NULL;
    L7session_ibmmq_private *p_st_private = NULL;
    Frbyte_Packet *p_st_data = NULL;
    unsigned int ui_data_len = 0;
    char arr_report_pre[32];
    int i_type = 0;
    int i_ret = FR_SUCC;
    memset(arr_report_pre, 0x00, 32);

    i_ret = ibmmq_put_analysis(p_st_body_data, p_st_private, p_st_data, ui_data_len, arr_report_pre, i_type);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    return;
}

static void case2(void)
{
    L7_head_ibmmq *p_st_head = NULL;
    char *p_data = NULL;
    int i_len = 0;

    Frreport_body_data *p_st_body_data = NULL;
    L7session_ibmmq_private *p_st_private = NULL;
    Frbyte_Packet *p_st_data = NULL;
    unsigned int ui_data_len = 0;
    char arr_report_pre[32];
    int i_type = 0;
    int i_ret = FR_SUCC;
    memset(arr_report_pre, 0x00, 32);
    L7Session_ibmmq *p_st_L7_session = NULL;
    L7_fragment_ibmmq *p_st_fragment = NULL;

    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;
    p_st_private = (L7session_ibmmq_private *)frmalloc(sizeof(L7session_ibmmq_private));
    memset(p_st_private, 0x00, sizeof(L7session_ibmmq_private));

    p_st_L7_session = (L7Session_ibmmq *)frmalloc(sizeof(L7Session_ibmmq));
    memset(p_st_L7_session, 0x00, sizeof(L7Session_ibmmq));
    p_st_private->p_st_l7_session = p_st_L7_session;

    p_st_fragment = (L7_fragment_ibmmq *)frmalloc(sizeof(L7_fragment_ibmmq));
    memset(p_st_fragment, 0x00, sizeof(L7_fragment_ibmmq));
    p_st_fragment->u64_total_len = 0;
    p_st_private->p_st_fragment = p_st_fragment;

    read_file((char *)"ibmmq_put_analysis2.pcap", &p_data, &i_len);

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, p_data+36, i_len-36);

    ibmmq_analysis_head(p_data, 36, &p_st_head);
    frfree(p_data);
    p_st_head->ui_head_len = 36;
    p_st_private->p_st_head = p_st_head;

    i_ret = ibmmq_put_analysis(p_st_body_data, p_st_private, p_st_data, ui_data_len, arr_report_pre, i_type);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_TODO);

    frreport_body_free(&p_st_body_data);
    bytebuff_free(&p_st_data);
    ibmmq_session_destroy(p_st_private);

    return;
}

static void case3(void)
{
    L7_head_ibmmq *p_st_head = NULL;
    char *p_data = NULL;
    int i_len = 0;

    Frreport_body_data *p_st_body_data = NULL;
    L7session_ibmmq_private *p_st_private = NULL;
    Frbyte_Packet *p_st_data = NULL;
    char arr_report_pre[32];
    int i_type = 0;
    int i_ret = FR_SUCC;
    memset(arr_report_pre, 0x00, 32);
    L7Session_ibmmq *p_st_L7_session = NULL;
    L7_fragment_ibmmq *p_st_fragment = NULL;

    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;
    p_st_private = (L7session_ibmmq_private *)frmalloc(sizeof(L7session_ibmmq_private));
    memset(p_st_private, 0x00, sizeof(L7session_ibmmq_private));

    p_st_L7_session = (L7Session_ibmmq *)frmalloc(sizeof(L7Session_ibmmq));
    memset(p_st_L7_session, 0x00, sizeof(L7Session_ibmmq));
    p_st_private->p_st_l7_session = p_st_L7_session;

    p_st_fragment = (L7_fragment_ibmmq *)frmalloc(sizeof(L7_fragment_ibmmq));
    memset(p_st_fragment, 0x00, sizeof(L7_fragment_ibmmq));
    p_st_fragment->u64_total_len = 0;
    p_st_private->p_st_fragment = p_st_fragment;

    read_file((char *)"ibmmq_put_analysis3.pcap", &p_data, &i_len);

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, p_data+36, i_len-36);

    ibmmq_analysis_head(p_data, 36, &p_st_head);
    frfree(p_data);
    p_st_head->ui_head_len = 36;
    p_st_private->p_st_head = p_st_head;

    i_ret = ibmmq_put_analysis(p_st_body_data, p_st_private, p_st_data, i_len-36, arr_report_pre, i_type);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    frreport_body_free(&p_st_body_data);
    bytebuff_free(&p_st_data);
    ibmmq_session_destroy(p_st_private);
    
    return;
}

static void case4(void)
{
    L7_head_ibmmq *p_st_head = NULL;
    char *p_data = NULL;
    int i_len = 0;

    Frreport_body_data *p_st_body_data = NULL;
    L7session_ibmmq_private *p_st_private = NULL;
    Frbyte_Packet *p_st_data = NULL;
    char arr_report_pre[32];
    int i_type = 0;
    int i_ret = FR_SUCC;
    memset(arr_report_pre, 0x00, 32);
    L7Session_ibmmq *p_st_L7_session = NULL;
    L7_fragment_ibmmq *p_st_fragment = NULL;

    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;
    p_st_private = (L7session_ibmmq_private *)frmalloc(sizeof(L7session_ibmmq_private));
    memset(p_st_private, 0x00, sizeof(L7session_ibmmq_private));

    p_st_L7_session = (L7Session_ibmmq *)frmalloc(sizeof(L7Session_ibmmq));
    memset(p_st_L7_session, 0x00, sizeof(L7Session_ibmmq));
    p_st_private->p_st_l7_session = p_st_L7_session;

    p_st_fragment = (L7_fragment_ibmmq *)frmalloc(sizeof(L7_fragment_ibmmq));
    memset(p_st_fragment, 0x00, sizeof(L7_fragment_ibmmq));
    p_st_fragment->u64_total_len = 0;
    p_st_private->p_st_fragment = p_st_fragment;

    read_file((char *)"ibmmq_put_analysis4.pcap", &p_data, &i_len);

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, p_data+36, i_len-36);
    //p_st_data_tmp = p_st_data;

    ibmmq_analysis_head(p_data, 36, &p_st_head);
    frfree(p_data);
    p_st_head->ui_head_len = 36;
    p_st_private->p_st_head = p_st_head;

    i_ret = ibmmq_put_analysis(p_st_body_data, p_st_private, p_st_data, i_len-36, arr_report_pre, i_type);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    //print_frlist(p_lst_header);

    frreport_body_free(&p_st_body_data);
    bytebuff_free(&p_st_data);
    ibmmq_session_destroy(p_st_private);

    return;
}

static void case5(void)
{
    L7_head_ibmmq *p_st_head = NULL;
    char *p_data = NULL;
    int i_len = 0;

    Frreport_body_data *p_st_body_data = NULL;
    L7session_ibmmq_private *p_st_private = NULL;
    Frbyte_Packet *p_st_data = NULL;
    char arr_report_pre[32];
    int i_type = 0;
    int i_ret = FR_SUCC;
    memset(arr_report_pre, 0x00, 32);
    L7Session_ibmmq *p_st_L7_session = NULL;
    L7_fragment_ibmmq *p_st_fragment = NULL;

    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;
    p_st_private = (L7session_ibmmq_private *)frmalloc(sizeof(L7session_ibmmq_private));
    memset(p_st_private, 0x00, sizeof(L7session_ibmmq_private));

    p_st_L7_session = (L7Session_ibmmq *)frmalloc(sizeof(L7Session_ibmmq));
    memset(p_st_L7_session, 0x00, sizeof(L7Session_ibmmq));
    p_st_private->p_st_l7_session = p_st_L7_session;

    p_st_fragment = (L7_fragment_ibmmq *)frmalloc(sizeof(L7_fragment_ibmmq));
    memset(p_st_fragment, 0x00, sizeof(L7_fragment_ibmmq));
    p_st_fragment->u64_total_len = 0;
    p_st_private->p_st_fragment = p_st_fragment;

    read_file((char *)"ibmmq_put_analysis5.pcap", &p_data, &i_len);

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, p_data+36, i_len-36);
    //p_st_data_tmp = p_st_data;

    ibmmq_analysis_head(p_data, 36, &p_st_head);
    frfree(p_data);
    p_st_head->ui_head_len = 36;
    p_st_private->p_st_head = p_st_head;

    i_ret = ibmmq_put_analysis(p_st_body_data, p_st_private, p_st_data, i_len-36, arr_report_pre, i_type);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    //print_frlist(p_lst_header);

    frreport_body_free(&p_st_body_data);
    bytebuff_free(&p_st_data);
    ibmmq_session_destroy(p_st_private);

    return;
}

static void case6(void)
{
    L7_head_ibmmq *p_st_head = NULL;
    char *p_data = NULL;
    int i_len = 0;

    Frreport_body_data *p_st_body_data = NULL;
    L7session_ibmmq_private *p_st_private = NULL;
    Frbyte_Packet *p_st_data = NULL;
    char arr_report_pre[32];
    int i_type = 0;
    int i_ret = FR_SUCC;
    memset(arr_report_pre, 0x00, 32);
    L7Session_ibmmq *p_st_L7_session = NULL;
    L7_fragment_ibmmq *p_st_fragment = NULL;

    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;
    p_st_private = (L7session_ibmmq_private *)frmalloc(sizeof(L7session_ibmmq_private));
    memset(p_st_private, 0x00, sizeof(L7session_ibmmq_private));

    p_st_L7_session = (L7Session_ibmmq *)frmalloc(sizeof(L7Session_ibmmq));
    memset(p_st_L7_session, 0x00, sizeof(L7Session_ibmmq));
    p_st_private->p_st_l7_session = p_st_L7_session;

    p_st_fragment = (L7_fragment_ibmmq *)frmalloc(sizeof(L7_fragment_ibmmq));
    memset(p_st_fragment, 0x00, sizeof(L7_fragment_ibmmq));
    p_st_fragment->u64_total_len = 0;
    p_st_private->p_st_fragment = p_st_fragment;

    read_file((char *)"ibmmq_put_analysis6.pcap", &p_data, &i_len);

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, p_data+36, i_len-36);
    //p_st_data_tmp = p_st_data;

    ibmmq_analysis_head(p_data, 36, &p_st_head);
    frfree(p_data);
    p_st_head->ui_head_len = 36;
    p_st_private->p_st_head = p_st_head;

    i_ret = ibmmq_put_analysis(p_st_body_data, p_st_private, p_st_data, i_len-36, arr_report_pre, i_type);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    //print_frlist(p_lst_header);

    frreport_body_free(&p_st_body_data);
    bytebuff_free(&p_st_data);
    ibmmq_session_destroy(p_st_private);

    return;
}

int add_suite_ibmmq_put_analysis(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" ibmmq_put_analysis()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL ||
        CU_add_test(pSuite, "case2", case2) == NULL ||
        CU_add_test(pSuite, "case3", case3) == NULL ||
        CU_add_test(pSuite, "case4", case4) == NULL ||
        CU_add_test(pSuite, "case5", case5) == NULL ||
        CU_add_test(pSuite, "case6", case6) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
